package yxy.game.pm2.command.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.apple.util.Base64Utility;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.charge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.*;
import yxy.game.pm2.active.module.actfavoritewarlord.FavouritePackage;
import yxy.game.pm2.active.module.guanyurecruit.ActiveModule_关羽招募;
import yxy.game.pm2.active.module.paylevelpackage.ActiveModule_支付等级礼包;
import yxy.game.pm2.active.module.subordinateclass.DevelopGiftPackage;
import yxy.game.pm2.active.module.subordinateclass.FlashSale;
import yxy.game.pm2.active.module.subordinateclass.GiftPackage;
import yxy.game.pm2.active.module.subordinateclass.RecruitPackage;
import yxy.game.pm2.bean.OrderUrl;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.PayInfoModule;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PayMessage;
import yxy.game.pm2.module.pay.PostUtil;

/**
 * 支付订单请求
 *
 * @author Dell
 */
@MessageType(proto = Command.Order, classtype = PayMessage.OrderRequest.class)
public class CMD_CreateOrder extends Command<PayMessage.OrderRequest> {

    private static final Logger logger = Logger.create(CMD_CreateOrder.class);

    @Override
    public void execute(MessageSource messageSource, PayMessage.OrderRequest request) throws Exception {
        logger.info("CMD_CreateOrder:%s:%s:%s:%s", request.getChannel(), request.getGoodsid(), request.getSystem(), request.getExt());
        String pcode = messageSource.getTag();
        int goodsid = request.getGoodsid();
        String channel = request.getChannel();
        int system = request.getSystem();

        PayMessage.OrderResponse.Builder response = PayMessage.OrderResponse.newBuilder();

        PlayerBean pb = PlayerBean.get(pcode);
        int sid = pb.getServerId();

        charge c = DataCenter.getData(goodsid, charge.class);

        if (c == null) {
            response.setOrderinfo("{}");
            send(messageSource, response, Result.PayNoGoods);
            return;
        }

        int acttype = 0;

        // 判断能否购买 TODO:
        switch (c.getType()) {
            case PayInfoModule.普通充值: {
                acttype = 0;
                break;
            }
            case PayInfoModule.每日礼包: {
                ActiveModule_每日礼包 am = ActiveModule_每日礼包.get(pcode, false);
                if (!am.canbuy(goodsid)) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.每日礼包;
                break;
            }
            case PayInfoModule.每周特惠: {
                ActiveModule_每周特惠 am = ActiveModule_每周特惠.get(pcode, false);
                if (!am.canbuy(goodsid)) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.每周特惠;
                break;
            }
            case PayInfoModule.每月特惠: {
                ActiveModule_每月特惠 am = ActiveModule_每月特惠.get(pcode, false);
                if (!am.canbuy(goodsid)) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.每月特惠;
                break;
            }
            case PayInfoModule.特权商城: {
                ActiveModule_特权商城 am = ActiveModule_特权商城.get(pcode);
                if (!am.canbuy2(goodsid)) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }
                acttype = ActType.特权商城;
                break;
            }
            case PayInfoModule.成长基金: {
                ActiveModule_成长基金 am = ActiveModule_成长基金.get(pcode);
                if (!am.getBuytime().isEqual(new DateTime(0))) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }
                acttype = ActType.成长基金;
                break;
            }
            case PayInfoModule.限时抢购: {
                ActiveModule_限时抢购 am = ActiveModule_限时抢购.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                for (FlashSale fs : am.getFss()) {
                    if (fs.getGoodsid() == c.getId()) {
                        if (fs.getBuynum() + fs.getOrderidtimemap().size() >= fs.getBuylimitnum()) {
                            response.setOrderinfo("{}");
                            send(messageSource, response, Result.PayCannotBuy);
                            return;
                        }
                    }
                }

                acttype = ActType.限时抢购;
                break;
            }
            case PayInfoModule.养成礼包: {
                ActiveModule_养成礼包 am = ActiveModule_养成礼包.get(pcode);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                if (!am.running()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                for (DevelopGiftPackage dgift : am.getDgifts()) {
                    if (dgift.getGoodsid() == c.getId()) {
                        if (dgift.isHadbuy() || dgift.isLock()) {
                            response.setOrderinfo("{}");
                            send(messageSource, response, Result.PayCannotBuy);
                            return;
                        }
                    }
                }

                acttype = ActType.养成礼包;
                break;
            }
            case PayInfoModule.超值月基金: {
                ActiveModule_超值月基金 am = ActiveModule_超值月基金.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                if (!am.running()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int type = am.getTyprOrGoodsid(0, goodsid);
                int canget = am.canBuy(type);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.超值月基金;
                break;
            }
            case PayInfoModule.限时礼包: {
                ActiveModule_限时礼包_Person am = ActiveModule_限时礼包_Person.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int canget = am.canBuy(goodsid);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.限时礼包;
                break;
            }
            case PayInfoModule.关卡基金: {
                ActiveModule_关卡基金 am = ActiveModule_关卡基金.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int canget = am.canbuy(goodsid);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.关卡基金;
                break;
            }
            case PayInfoModule.每日特惠: {
                ActiveModule_每日特惠 am = ActiveModule_每日特惠.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int canget = am.canbuy(goodsid);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.每日特惠;
                break;
            }
            case PayInfoModule.周卡: {
                ActiveModule_周卡 am = ActiveModule_周卡.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int canget = am.canbuy(goodsid);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.周卡;
                break;
            }
            case PayInfoModule.神魔卡: {
                ActiveModule_神魔卡 am = ActiveModule_神魔卡.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int canget = am.canbuy(goodsid);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.神魔卡;
                break;
            }
            case PayInfoModule.翻牌: {
                ActiveModule_翻牌 am = ActiveModule_翻牌.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int canget = am.canbuy(goodsid);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.翻牌;
                break;
            }
            case PayInfoModule.支付等级礼包: {
                ActiveModule_支付等级礼包 am = ActiveModule_支付等级礼包.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int canget = am.canBuyGiftPackage(goodsid);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.支付等级礼包;
                break;
            }
            case PayInfoModule.关羽招募: {
                ActiveModule_关羽招募 am = ActiveModule_关羽招募.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                int canget = am.buy(goodsid);

                if (canget == DrawStatus.hadget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }

                if (canget == DrawStatus.cannotget.getStatus()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }

                acttype = ActType.关羽招募;
                break;
            }
            case PayInfoModule.节日活动: {
                ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }
                DrawStatus canget = am.buy(goodsid);
                if (canget == DrawStatus.hadget) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActHadGet);
                    return;
                }
                if (canget == DrawStatus.cannotget) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.PayCannotBuy);
                    return;
                }
                acttype = ActType.节日活动商城;
                break;
            }
            case PayInfoModule.招募令礼包: {
                ActiveModule_招募令礼包 am = ActiveModule_招募令礼包.get(pcode);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                if (!am.running()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                for (RecruitPackage dgift : am.getDgifts()) {

                    if (dgift.getGoodsid() == c.getId()) {
                        if (dgift.getBuyNum() >= dgift.getNumlimit() || dgift.isLock()) {
                            response.setOrderinfo("{}");
                            send(messageSource, response, Result.PayCannotBuy);
                            return;
                        }
                    }
                }

                acttype = ActType.招募令礼包;
                break;
            }
            case PayInfoModule.武将直升: {
                ActiveModule_武将直升活动 am = ActiveModule_武将直升活动.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                if (!am.running()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                //判断商品是否已经达到购买限制
                for (GiftPackage dgift : am.getDgifts()) {
                    if (dgift.getGoodsid() == c.getId()) {
                        if (dgift.getBuyNum() >= dgift.getNumlimit()) {
                            response.setOrderinfo("{}");
                            send(messageSource, response, Result.PayCannotBuy);
                            return;
                        }
                    }
                }
                acttype = ActType.武将直升;
                break;
            }
            case PayInfoModule.心仪武将: {
                ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                if (!am.running()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                //判断商品是否已经达到购买限制
                for (FavouritePackage dgift : am.getDgifts()) {
                    if (dgift.getGoodsid() == c.getId()) {
                        if (dgift.getBuyNum() >= dgift.getNumlimit()) {
                            response.setOrderinfo("{}");
                            send(messageSource, response, Result.PayCannotBuy);
                            return;
                        }
                    }
                }
                acttype = ActType.心仪武将;
                break;
            }
            case PayInfoModule.心愿瓶: {
                ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                if (!am.running()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                //判断商品是否已经达到购买限制
                for (FavouritePackage dgift : am.getDgifts()) {
                    if (dgift.getGoodsid() == c.getId()) {
                        if (dgift.getBuyNum() >= dgift.getNumlimit()) {
                            response.setOrderinfo("{}");
                            send(messageSource, response, Result.PayCannotBuy);
                            return;
                        }
                    }
                }
                acttype = ActType.心愿瓶;
                break;
            }
            case PayInfoModule.国庆寻宝: {
                ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(pcode, false);
                if (am == null) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                if (!am.running()) {
                    response.setOrderinfo("{}");
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }

                //判断商品是否已经达到购买限制
                for (FavouritePackage dgift : am.getDgifts()) {
                    if (dgift.getGoodsid() == c.getId()) {
                        if (dgift.getBuyNum() >= dgift.getNumlimit()) {
                            response.setOrderinfo("{}");
                            send(messageSource, response, Result.PayCannotBuy);
                            return;
                        }
                    }
                }
                acttype = ActType.国庆寻宝;
                break;
            }
        }

        JSONObject postjson = new JSONObject();
        postjson.put("pcode", pcode);
        postjson.put("goodsid", goodsid);
        postjson.put("sid", sid);
        postjson.put("charge", c);
        postjson.put("channel", channel);
        postjson.put("acttype", acttype);
        postjson.put("vipupexp", c.getVipexp());
        postjson.put("uid", pb.getUid());
        postjson.put("ext", request.getExt());
        if (system != 0) {
            postjson.put("system", system);
        }

        String url = OrderUrl.orderurl();
        String ret = null;
        try {
            NameValuePair[] pair = PostUtil.getValuePair(postjson);
            ret = PostUtil.doPost(url, pair, 30000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info(getClass().getSimpleName() + " ret:" + ret);

        if (ret == null || ret.equals("")) {
            response.setOrderinfo("{}");
            send(messageSource, response, Result.DataError);
            return;
        }

        ret = new String(Base64Utility.decode(ret.getBytes())); //Base64Utility.decode(ret.getBytes()).toString();
        logger.info("解码：" + " ret:" + ret);
        JSONObject json = JSON.parseObject(ret);

        // 锁单 TODO:
        switch (c.getType()) {
            case PayInfoModule.每日礼包: {
                ActiveModule_每日礼包 am = ActiveModule_每日礼包.get(pcode, false);
                String myorderid = json.getString("myorderid");
                am.locknumnfdataid(goodsid, myorderid);
                break;
            }
            case PayInfoModule.每周特惠: {
                ActiveModule_每周特惠 am = ActiveModule_每周特惠.get(pcode, false);
                String myorderid = json.getString("myorderid");
                am.locknumnfdataid(goodsid, myorderid);
                break;
            }
            case PayInfoModule.每月特惠: {
                ActiveModule_每月特惠 am = ActiveModule_每月特惠.get(pcode, false);
                String myorderid = json.getString("myorderid");
                am.locknumnfdataid(goodsid, myorderid);
                break;
            }
            case PayInfoModule.特权商城: {
                ActiveModule_特权商城 am = ActiveModule_特权商城.get(pcode);
                String myorderid = json.getString("myorderid");
                am.lock(goodsid, myorderid);
                break;
            }
            case PayInfoModule.限时抢购: {
                ActiveModule_限时礼包_Person am = ActiveModule_限时礼包_Person.get(pcode, false);
                String myorderid = json.getString("myorderid");
                am.lockorder(goodsid, myorderid);
                break;
            }
            case PayInfoModule.关卡基金: {
                ActiveModule_关卡基金 am = ActiveModule_关卡基金.get(pcode, false);
//				String myorderid = json.getString("myorderid");
//				am.dealbuy(goodsid);
                am.lockorder(goodsid);
                break;
            }
            case PayInfoModule.周卡: {
                ActiveModule_周卡 am = ActiveModule_周卡.get(pcode, false);
                String myorderid = json.getString("myorderid");
                am.lockorder(goodsid, myorderid);
                break;
            }
            case PayInfoModule.神魔卡: {
                ActiveModule_神魔卡 am = ActiveModule_神魔卡.get(pcode, false);
                String myorderid = json.getString("myorderid");
                am.lockorder(goodsid, myorderid);
                break;
            }
            case PayInfoModule.翻牌: {
                ActiveModule_翻牌 am = ActiveModule_翻牌.get(pcode, false);
                String myorderid = json.getString("myorderid");
                am.lockorder(goodsid, myorderid);
                break;
            }
            case PayInfoModule.关羽招募: {
                ActiveModule_关羽招募 am = ActiveModule_关羽招募.get(pcode, false);
                am.lockorder(goodsid);
                break;
            }
            case PayInfoModule.节日活动: {
                ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode, false);
                String myorderid = json.getString("myorderid");
                am.lockorder(goodsid, myorderid);
                break;
            }
        }

        try {
            if (c.getName().equals("限时抢购")) {
                ActiveModule_限时抢购 am = ActiveModule_限时抢购.get(pcode, false);
                if (am != null) {
                    am.lockOrder(goodsid, json.getString("myorderid"));
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        logger.info("orderinfo:" + json);
        response.setOrderinfo(json.toString());
        send(messageSource, response, Result.Success);

    }

    private void send(MessageSource messageSource, PayMessage.OrderResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
